<template>
  <div class="home-container">
    <!-- 搜索框 -->
    <van-search
      shape="round"
      background="#c82519"
      placeholder="请输入搜索关键词"
    />
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <!-- 轮播图 -->
      <van-swipe class="swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item
          class="swipe-item"
          v-for="item in bannerList"
          :key="item.bannerid"
        >
          <img :src="item.img" />
        </van-swipe-item>
      </van-swipe>
      <!-- 菜单列表 -->
      <van-grid :column-num="5" :border="false">
        <van-grid-item
          v-for="item in menuList"
          :key="item.id"
          :icon="item.icon"
          :text="item.title"
        />
      </van-grid>
      <!-- 商品列表 -->
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getGoodsListData"
        class="goods-list"
      >
        <router-link
          class="goods-item"
          v-for="item in goodsList"
          :key="item.proid"
          :to="'/detail?proid=' + item.proid"
        >
          <!-- <img :src="item.img1" /> -->
          <img v-lazy="item.img1" />
          <div class="title">{{ item.proname }}</div>
          <div class="desc">{{ item.desc }}</div>
          <div class="price">￥{{ item.originprice }}</div>
        </router-link>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup>
import { getBannerList, getMenuList, getGoodsList } from '@/api/home'
import { ref } from 'vue'

// 定义模型数据
const bannerList = ref([])
const menuList = ref([])
const goodsList = ref([])
const loading = ref(false) // 上拉加载更多loading
const finished = ref(false) // 是否完成了的loading
const refreshing = ref(false) // 下拉刷新loading
let count = 0 // 页码

const getBannerListData = async () => {
  const res = await getBannerList()
  bannerList.value = res.data
}

const getMenuListData = async () => {
  const res = await getMenuList()
  menuList.value = res.data
}

const getGoodsListData = async () => {
  count++
  const res = await getGoodsList({
    count,
    limitNum: 10
  })

  goodsList.value = [...goodsList.value, ...res.data]

  // 没有数据啦
  if (res.data.length === 0) {
    finished.value = true
  }

  // 每次加载完毕，把上拉加载更多的loading和下拉刷新的loading都设置为false
  loading.value = false
  refreshing.value = false
}

const onRefresh = () => {
  goodsList.value = []
  finished.value = false
  count = 0
  getGoodsListData()
}

getBannerListData()
getMenuListData()
// getGoodsListData()
</script>

<style lang="scss" scoped>
.home-container {
  padding-bottom: 50px;
  .swipe {
    width: 100%;
    height: 250px;
  }
  .swipe-item {
    width: 100%;
    height: 250px;
  }
  .swipe-item img {
    width: 100%;
    height: 100%;
  }

  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    box-sizing: border-box;
    width: 100%;

    .goods-item {
      box-sizing: border-box;
      background-color: #fff;
      padding: 5.33333vw;
      border-radius: 1.33333vw;
      display: flex;
      align-items: center;
      width: 48%;
      flex-direction: column;
      margin-top: 1.33333vw;
      img {
        width: 26.66667vw;
        height: 32vw;
      }
      .title {
        font-size: 3.73333vw;
        display: inline-block;
        white-space: nowrap;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-top: 4vw;
        color: #000;
      }
      .desc {
        font-size: 3.2vw;
        color: #c3c3c3;
        display: inline-block;
        white-space: nowrap;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-top: 2.66667vw;
      }
      .price {
        font-size: 3.2vw;
        margin-top: 2.66667vw;
        color: red;
      }
    }
  }
}
// 深度作用选择器
::v-deep(.van-list .van-list__loading) {
  width: 100%;
}
::v-deep(.van-list__finished-text) {
  width: 100%;
  text-align: center;
}
</style>
